package soul.resource;

import soul.domain.code.Status;
import soul.domain.response.Response;
import soul.entity.Meta;
import soul.entity.Org;
import soul.entity.User;
import soul.filter.AuthFilter;
import soul.mapper.MetaDao;
import soul.mapper.OrgDao;
import soul.utils.IDGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import soul.service.MetaService;
import soul.service.OrgService;

import java.util.*;

/**
 * Created by admin on 2016/11/26.
 */
@Controller
@RequestMapping("/meta")
public class MetaResource {

    @Autowired
    private MetaService metaService;
    @Autowired
    private OrgService orgService;
    @Autowired
    private MetaDao metaDao;
    @Autowired
    private OrgDao orgDao;

    @RequestMapping("/list")
    @ResponseBody
    public Response list() {
        User user = (User) AuthFilter.getVar("user");
        Assert.notNull(user, "没有权限");
        Assert.notNull(user.getType(), "没有权限");
        if (Objects.equals(user.getType(), User.TYPE_ORG)) {//组织管理员
            Map<String, Object> orgParam = new HashMap<>();
            orgParam.put("userId", user.getId());
            List<Long> orgIds = new ArrayList<>();
            List<Org> orgs = orgService.select(orgParam);
            orgs.forEach(org -> {
                orgIds.add(org.getId());
            });
            Map<String, Object> metaParam = new HashMap<>();
            metaParam.put("orgIds", orgIds);
            metaParam.put("type", User.TYPE_ORG);
            List<Meta> list = metaService.select(metaParam);
            return Response.ok(Status.Ok, list);
        } else if (user.getType().equals(User.TYPE_SUPPER)) {
            Map<String, Object> metaParam = new HashMap<>();
            metaParam.put("type", User.TYPE_SUPPER);
            metaParam.put("orgIds", new ArrayList<>());
            List<Meta> list = metaService.select(metaParam);
            return Response.ok(Status.Ok, list);
        } else {
            throw new RuntimeException("type不对");
        }
    }

    @RequestMapping("/guest_list")
    @ResponseBody
    public Response metaList(@RequestBody Map map) {
        Map<String, Object> filter = new HashMap<>();
        filter.put("orgId",map.get("orgId"));
        List<Meta> list = metaDao.select(filter);
        return Response.ok(Status.Ok, list);

    }

    @RequestMapping("/add")
    @ResponseBody
    public Response add(@RequestBody Meta meta) {
        Map<String,Object> filter = new HashMap<>();
        filter.put("id",meta.getOrgId());
        Assert.notNull(orgDao.selectByOrgId(filter),"组织不存在");
        meta.setId(IDGenerator.generateId());
        meta.setStatus(0);
        meta.setCreateDt(new Date());
        metaService.addMeta(meta);
        return Response.ok(Status.Ok);
    }

    @RequestMapping("/guest_add")
    @ResponseBody
    public Response guestAdd(@RequestBody Meta meta) {
        Map<String,Object> filter = new HashMap<>();
        filter.put("id",meta.getOrgId());
        Org org = orgDao.selectByOrgId(filter);
        Assert.notNull(org,"组织不存在");
        //todo jiami
        Assert.isTrue(org.getPassword().equalsIgnoreCase(meta.getPassword()),"密码不正确");
        meta.setId(IDGenerator.generateId());
        meta.setStatus(0);
        meta.setCreateDt(new Date());
        metaService.addMeta(meta);
        return Response.ok(Status.Ok);
    }

    @RequestMapping("/confirm")
    @ResponseBody
    public Response confirm(@RequestBody Meta meta) {
        metaService.confirm(meta);
        return Response.ok(Status.Ok);
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Response delete(@RequestBody Meta meta) {
        metaService.delete(meta);
        return Response.ok(Status.Ok);
    }
}
